home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1991 …esperately Seeking Seven / Desperately Seeking Seven.2mg / Dev.CD.8 / Essentials / Tools / DTS.Samples / SC20DTSToolLib / PtrCheck.asm < prev    next >
Encoding:
Assembly Source File  |  1990-04-06  |  11.0 KB  |  379 lines  |  [04] ASCII Text (0x0000)

  1. ******************************************************************************
  2. *
  3. * lclptr.checker
  4. *
  5. * Copyright (C) 1989 by Apple Computer.
  6. * Written by Eric Soldan.
  7. *
  8. * Converted to APW 6-May-90 DAL
  9. *
  10. ******************************************************************************
  11.  
  12.                case on
  13.  
  14. rtlStkDepth    gequ 50            ;You may need to make this bigger.
  15.  
  16. ******************************************************************************
  17. *
  18. * These functions are to help detect usage of C pointers and handles that are
  19. * not initialized.  In main(), call initPtrCheck().  Do NOT call other
  20. * functions in main.
  21. * In other functions, call zapLocals() as the first thing in the function.
  22. * checkForHit() will automatically be called whenever you exit a function.
  23. * You can call checkForHit() at any point you want, also.  If you have an idea
  24. * of which function has a pointer problem, but you are not sure where in
  25. * the function the problem is, you can add some calls to checkForHit().
  26. * The functions initPtrCheck(), closePtrCheck(), zapLocals(), and
  27. * checkForHit() can be conditionally set active or inactive with #define
  28. * statements.  This way they can be active when developing, and turned off
  29. * when making a production version.  The calls can be defined to nothing,
  30. * thus making any reference to them drop out of the object code.
  31. *
  32. * So, a C program would use the functions as below:
  33. *
  34. * main()
  35. * {
  36. *     initPtrCheck(_ownerid);
  37. *     /* Your code goes here */
  38. *     closePtrCheck();
  39. *     exit(0);
  40. * }
  41. *
  42. * void           someFnCalledByMain(parm1, parm2)
  43. * unsigned int   parm1, parm2;
  44. * {
  45. *     unsigned int   i, j, k;
  46. *     unsigned long  theID;
  47. *
  48. *     zapLocals();
  49. *     /* Your code goes here */
  50. *     checkForHit();
  51. * }
  52. *
  53. * Note that zapLocals() needs to be the first code in a function.  Also,
  54. * you can't call zapLocals before you call initPtrCheck().  Given this
  55. * dependency, you can't call zapLocals in main().  To fix this problem,
  56. * you may wish to have a nearly empty main().  You may want it to just
  57. * do the initPtrCheck() and closePtrCheck(), and the code in between
  58. * simply calls something like main0(), which can have a zapLocals().
  59. *
  60. ******************
  61.  
  62. * The return address to exit zapLocals is used to point to the code used to
  63. * create the stack frame.  The stack frame code should look like the below.
  64. * The values in front are the negative offsets from the return address.
  65. * (This is why zapLocals() must be the first code in a function.)
  66.  
  67. * E            PHD                 
  68. * D            TSC                 
  69. * C            SEC                 
  70. * BA9          SBC  #$fb
  71. * 8            TCD                 
  72. * 765          ADC  #$f0
  73. * 4            TCS                 
  74. *
  75. * 321.0        JSL  >zapLocals
  76.  
  77. ******************************************************************************
  78.  
  79.                mcopy 2/ainclude/m16.memory
  80.                mcopy 2/ainclude/m16.misctool
  81.                mcopy 2/ainclude/m16.util2
  82.  
  83.  
  84. initPtrCheck   start
  85.  
  86.                DefineStack
  87.  
  88. hndl           long               ;Must be at 1,s.
  89. ptr            long
  90.  
  91. dpage          word
  92. retaddr        block 3
  93.  
  94. userid         word
  95.  
  96.                phd
  97.  
  98.                pha                ;Make space for locals.
  99.                pha
  100.                pha
  101.                pha
  102.                tsc
  103.                tcd
  104.  
  105.                pea 1              ;Get the bank-sized handle.
  106.                lda #0
  107.                sta >rtlStkPtr     ;Nothing on "stack" yet.
  108.                pha
  109.                pei userid
  110.                pea $C010
  111.                pha
  112.                pha
  113.                _NewHandle
  114.                bcc gothndl
  115.  
  116.                lda #0
  117.                sta >ptrCheckHndl  ;Flag as no handle.
  118.                sta >ptrCheckHndl+2
  119.                sta >zapval        ;"Pattern" is 0.
  120.                sta >$0            ;Point 0-2 to the screen.
  121.                lda #$E100         ;If there is any bad stuff
  122.                sta >$1            ;going on, then it will show
  123.                bra exit1          ;on the super-hi-res screen.
  124. *                                 ;This is true only for handles.
  125. *                                 ;Simple pointers will still
  126. *                                 ;store into zpage.  Storing
  127. *                                 ;into zpage will, most likely,
  128. *                                 ;cause the system to die.
  129. *                                 ;The best test is when the
  130. *                                 ;system has enough memory to
  131. *                                 ;create the 64k hit-test handle.
  132.  
  133. gothndl        lda hndl           ;Keep the handle, and initialize it.
  134.                sta >ptrCheckHndl
  135.                lda hndl+2
  136.                sta >ptrCheckHndl+2
  137.  
  138.                ldy #2             ;Dereference the handle to get to
  139.                lda [hndl],y       ;the bank of memory.
  140.                sta ptr+2
  141.                lda [hndl]
  142.                sta ptr
  143.                lda ptr+2          ;Move byte 2 of bank address into
  144.                xba                ;byte 3 also.
  145.                ora ptr+2
  146.                sta >zapval
  147.  
  148.                ldy #0             ;Fill the handle with the bank value,
  149. loop           sta [ptr],y        ;so pointers within the handle end
  150.                iny                ;up pointing back into the handle.
  151.                iny
  152.                bne loop
  153.  
  154.                sta >$0
  155.                sta >$2
  156.  
  157. exit1          pla
  158.                pla
  159.                pla
  160.                pla
  161.  
  162.                pld
  163.                rtl
  164.  
  165. ptrCheckHndl   entry
  166.                dc i4'0'
  167. zapval         entry
  168.                dc i2'0'
  169. rtlStkPtr      entry
  170.                dc i2'0'
  171. rtlStk         entry
  172.                ds rtlStkDepth*3
  173.  
  174.                end
  175.  
  176. ******************
  177.  
  178. zapLocals      start
  179.  
  180.                DefineStack
  181.  
  182. ptr            long
  183.  
  184. dpage          word
  185. retaddr        block 3
  186.  
  187.                phd
  188.  
  189.                pha                ;Make space for locals.
  190.                pha
  191.                tsc
  192.                tcd
  193.  
  194.                lda retaddr+1      ;Use return address as pointer.
  195.                sta ptr+1
  196.                lda retaddr
  197.                sec
  198.                sbc #$0A
  199.                sta ptr
  200.                lda [ptr]          ;sbc value
  201.                tax
  202.                ldy #4
  203.                lda [ptr],y        ;adc value
  204.  
  205.                sta ptr
  206.                txa
  207.                sec
  208.                sbc ptr
  209.                beq exit2          ;No stack frame at all.
  210.                tax                ;Save this to fetch RTL address.
  211.                tay                ;Number of bytes to zap.
  212.                dey
  213.                beq exit2          ;Just in case.
  214.  
  215.                tsc
  216.                clc
  217.                adc #retaddr+2     ;Point to beginning of stack
  218.                sta ptr            ;frame to clobber.
  219.                stz ptr+2
  220.  
  221.                shortm
  222.                lda >zapval        ;Pattern to clobber with.
  223. loop2          sta [ptr],y
  224.                dey
  225.                bne loop2
  226.                longm
  227.  
  228.                inx                ;Save the return address on rtlStk.
  229.                inx
  230.                txy                ;yreg is offset to return address.
  231.                lda >rtlStkPtr
  232.                cmp #rtlStkDepth*3 ;Make sure we aren't overflowing rtlStk.
  233.                bcc stackOK
  234.                brk $AA
  235.  
  236. stackOK        tax
  237.                lda [ptr],y
  238.                sta >rtlStk,x
  239.                iny
  240.                inx
  241.                lda [ptr],y
  242.                sta >rtlStk,x      ;Return address now on rtlStk.
  243.                inx
  244.                inx
  245.                txa
  246.                sta >rtlStkPtr     ;Save new index into rtlStk.
  247.  
  248.                lda #rtlCheck|-8   ;Put return address of checking
  249.                sta [ptr],y        ;routine in place of regular
  250.                dey                ;return address.
  251.                lda #rtlCheck-1
  252.                sta [ptr],y
  253.  
  254. exit2          pla
  255.                pla
  256.  
  257.                pld
  258.                rtl
  259.  
  260. rtlCheck       anop               ;This is where we end up when a c
  261. *                                 ;function exits with an rtl.
  262.                jsl saveRegs       ;Keep registers for return value for c.
  263.  
  264.                lda >rtlStkPtr     ;Get the real return address onto the stack.
  265.                tax
  266.                dex
  267.                dex
  268.                lda >rtlStk,x
  269.                pha                ;We pushed middle and hi byte here.
  270.                phb
  271.                pla                ;We just pulled the middle byte.
  272.                dex
  273.                lda >rtlStk,x
  274.                pha                ;We pushed low and middle byte here, so
  275. *                                 ;we have a full rtl address on stack.
  276.                txa
  277.                sta >rtlStkPtr
  278.                jml checkForHitz
  279.  
  280. saveRegs       entry
  281.                sta >keepa         ;Keep everything.
  282.                txa
  283.                sta >keepx
  284.                tya
  285.                sta >keepy
  286.                php
  287.                php
  288.                pla
  289.                sta >keepp
  290.                rtl
  291.  
  292. getRegs        entry
  293.                lda >keepp
  294.                pha
  295.                lda >keepx
  296.                tax
  297.                lda >keepy
  298.                tay
  299.                lda >keepa
  300.                plp
  301.                plp
  302.                rtl
  303.  
  304. keepa          dc i2'0'
  305. keepx          dc i2'0'
  306. keepy          dc i2'0'
  307. keepp          dc i2'0'
  308.  
  309.                end
  310.  
  311. ******************
  312.  
  313. checkForHit    start
  314.  
  315.                jsl saveRegs
  316.  
  317. checkForHitz   entry
  318.                lda >zapval
  319.                beq exit3          ;When using zpage as hit
  320. *                                 ;detector, there is nothing
  321. *                                 ;we can check.  We just have
  322. *                                 ;to wait until the system
  323. *                                 ;dies a horrible death.  This
  324. *                                 ;horrible death should happen
  325. *                                 ;faster when clobbering zpage.
  326.                tax
  327.                and #$FFFE         ;So we can go by words.
  328.                tay
  329.                txa
  330.  
  331.                phb
  332.                pha
  333.                plb
  334.                plb
  335.  
  336. loop3          cmp |$0,y
  337.                bne oops1
  338.                iny
  339.                iny
  340.                bne loop3
  341.  
  342.                plb                ;Put data bank back.
  343. exit3          jml getRegs
  344.  
  345. oops1          plb
  346.  
  347.                brk $AB            ;Bank is indicated by acc.
  348. *                                 ;Offset into bank that was hit is
  349. *                                 ;in the yreg.
  350. *                                 ;The program can be resumed, but unless
  351. *                                 ;the hit is repaired, the program will
  352. *                                 ;break again.
  353.  
  354.                jml getRegs         ;Allow option of continuing from debugger.
  355.  
  356.                end
  357.  
  358. ******************
  359.  
  360. closePtrCheck  start
  361.                jsl checkForHit     ;Let's make sure there were no hits.
  362.  
  363.                lda >zapval
  364.                beq exit              ;No handle to dispose of.
  365.  
  366.                lda >ptrCheckHndl+2
  367.                pha
  368.                lda >ptrCheckHndl
  369.                pha
  370.                _DisposeHandle       ;We got rid of what we created.
  371.  
  372.                lda #0
  373.                sta >zapval
  374.  
  375. exit           rtl
  376.  
  377.                end
  378.